package com.solo_project;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ThoughtWorker on 1/16/14.
 */
public class League {
    private PrintStream printStream;
    private BufferedReader bufferedReader;
    private List<Player> playerList;
    private List<Coach> coachList;
    private String name1 = "Dan Park" ;
    private String team1 = "ThoughtWorkers" ;
    private Integer number1 = 1 ;
    private Integer age1 = 27;
    private String option1 = "List Players";
    private String option2 = "Find Player";
    private String option3 = "List Coaches";
    private String coachName = "Bill";
    private String title = "Head Coach";
    private String LINE = "****************************";

    public League(PrintStream printStream, BufferedReader bufferedReader, List<Player> playerList, List<Coach> coachList) {
        this.printStream = printStream;
        this.bufferedReader = bufferedReader;
        this.playerList = playerList;
        this.coachList = coachList;
    }

    public void go(){
        addPlayer(name1, team1, number1, age1);
        addCoach(coachName, team1, title);
        showList();
        try {
            menu();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void showList() {
        listPlayers();
        listCoaches();
    }

    private void addCoach(String name, String team, String title) {
        coachList.add(new Coach(name, team, title));
    }

    public void addPlayer(String name){
        playerList.add(new Player(name));
    }
    public void addPlayer(String name, String team, Integer number, Integer age){
        playerList.add(new Player(name, team, number, age));
    }

    public void listPlayers() {
        printStream.println("Players:");
        for(Player player: playerList){
            listPlayer(player);
        }
        printStream.println(LINE);
    }

    public void listCoaches() {
        printStream.println("Coaches:");
        for(Coach coach: coachList){
            listCoach(coach);
        }
        printStream.println(LINE);
    }

    private void listCoach(Coach coach) {
        printStream.print("Name:  ");
        printStream.println(coach.name());
        printStream.print("Team:  ");
        printStream.println(coach.team());
        printStream.print("Title: ");
        printStream.println(coach.title());
    }

    public void listPlayer(Player player) {
            printStream.print("Name:  ");
            printStream.println(player.name());
            printStream.print("Team:  ");
            printStream.println(player.team());
            printStream.print("Age: ");
            printStream.println(player.age());
            printStream.print("Number: ");
            printStream.println(player.number());
    }

    public void menu() throws IOException {
        Map<String, String> menu = new HashMap<String, String>();
        String choice;
            menu.put("1", option1);
            menu.put("2", option2);
            menu.put("3", option3);
            displayOptions();
            choice = receiveInput();
            if(menu.containsKey(choice)){
                executeChoice(menu.get(choice).toString());
            }
            else
                printStream.println("Invalid!");
    }
    public void displayOptions(){
        printStream.println("Please Select an Option");
        printStream.print("1:  ");
        printStream.println(option1);
        printStream.print("2:  ");
        printStream.println(option2);
        printStream.print("3:  ");
        printStream.println(option3);
    }

    public String receiveInput() throws IOException {
        return bufferedReader.readLine();
    }

    public void executeChoice(String choice) throws IOException {
        if(choice == option1)
            listPlayers();
        if(choice == option2)
            findPlayer();
        if(choice == option3)
            listCoaches();
    }

    public void findPlayer() throws IOException {
        printStream.println("Please Enter Name of Player");
        String name = receiveInput();
        printStream.println("Please Enter Player Number");
        Integer number = Integer.parseInt(receiveInput());
        for(Player player: playerList){
            if(player.name().equals(name) && player.number() == number)
                listPlayer(player);
            else
                printStream.println("Cannot Find");
        }
    }
}
